using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using One.Web;
using One.Framework;

using System.Collections.Generic;
using One.Web.UI.WebControls;
using Resources;
using Money.Interfaces;
using Money.Data;

public partial class Admin_SignIn : System.Web.UI.Page
{
    #region fields
    private IUserService userService = null;
    private string redirectUrl = null;
    #endregion

    #region behaviors
    protected void Page_Load(object sender, EventArgs e)
    {
        messageLabel.Visible = false;
        userService = ServiceFactory.Create<IUserService>();
        GetParams();
        AddRule();
        UpdateView();
    }

    protected void SignInButton_Click(object sender, EventArgs e)
    {
        if (validationManager.Validate())
        {
            User user = Authenticate(usernameTextBox.Text, passwordTextBox.Text);
            if (user != null)
            {
                string[] roles = GetRoles(user.Roles);
                WebSession.CurrentUser = user;
                FormsAuthenticationHelper.RedirectFromLoginPage(user.Username,
                                                                    roles,
                                                                    FormsAuthentication.DefaultUrl,
                                                                    false);
            }
            else
            {
                messageLabel.Visible = true;
            }
        }
    }
    #endregion

    #region private functions
    private void GetParams()
    {
        redirectUrl = QueryStringUtility.GetString("ReturnUrl", string.Empty);
    }

    private void UpdateView() 
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(redirectUrl))
            {
                Response.Redirect(FormsAuthentication.LoginUrl);
            }
        }
    }

    private void AddRule()
    {
        validationManager.AddRule(new ValueRequiredRule(usernameTextBox, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new ValueRequiredRule(passwordTextBox, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));

        validationManager.Notifier = new BalloonNotifier();
    }

    private string[] GetRoles(IList<Role> iList)
    {
        string[] result = new string[iList.Count];
        for (int i = 0; i < iList.Count; i++)
        {
            result[i] = Enum.GetName(typeof(RoleType), iList[i].RoleId);
        }
        return result;
    }

    private User Authenticate(string username, string password)
    {
        try
        {
            return userService.Authentication(username, password);
        }
        catch (Exception ex)
        {
            Logger.Error(ex);
        }
        return null;
    }
    #endregion
}
